package com.imoresoft.magic.app.weather;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import com.ambitor.grass.util.data.DataMap;
import com.ambitor.grass.util.data.IData;

public class WeatherUtil {

	public static IData getWeather(String cityId) {
		String jsonStr = null;
		jsonStr = HttpConnectionUtil
				.getHttpContent("http://m.weather.com.cn/data/" + cityId
						+ ".html");
		if (StringUtils.isBlank(jsonStr))
			return new DataMap();
		JSONObject obj = JSONObject.fromObject(jsonStr);
		return new DataMap(obj.getJSONObject("weatherinfo"));
	}

	public static Map<String, Index> getIndexes(String cityId) throws Exception {
		String indexHtml = getWeatherIndexHtml(cityId);
		Map<String, Index> map = new HashMap<String, Index>();
		String rex = "<dl style(.*?)</dl>";
		Matcher m = Pattern.compile(rex, Pattern.DOTALL).matcher(indexHtml);
		while (m.find()) {
			String indexDetail = m.group();
			String name = getIndexName(indexDetail);
			String id = IndexConstants.getColumnNameByIndexName(name);
			if (StringUtils.isBlank(id))
				continue;
			String level = getIndexLevel(indexDetail);
			String content = getIndexContent(indexDetail);
			Index index = new Index();
			index.setId(id);
			index.setLevel(level);
			index.setContent(content);
			index.setName(name);
			map.put(id, index);
		}

		return map;
	}

	private static String getWeatherIndexHtml(String cityId) throws Exception {
		String url = "http://www.weather.com.cn/weather/" + cityId + ".shtml?";
		String html = HttpConnectionUtil.getHttpContent(url);
		String rex = "<div class=\"todayLiving\" id=\"index\">(.*?)<div class=\"cleardiv\">";
		Matcher m = Pattern.compile(rex, Pattern.DOTALL).matcher(html);
		if (m.find()) {
			return m.group();
		}
		return null;
	}

	private static String getIndexName(String html) throws Exception {
		String rex = "<a (.*?)：";
		Matcher m = Pattern.compile(rex, Pattern.DOTALL).matcher(html);
		if (m.find()) {
			String s = m.group();
			// 判断中文
			String rex2 = "[\u4e00-\u9fa5]+";
			Matcher m2 = Pattern.compile(rex2, Pattern.DOTALL).matcher(s);
			if (m2.find()) {
				return m2.group();
			}
		}
		return null;
	}

	private static String getIndexLevel(String html) throws Exception {
		String rex = "<b class(.*?)</b>";
		Matcher m = Pattern.compile(rex, Pattern.DOTALL).matcher(html);
		if (m.find()) {
			String str = m.group();
			// 判断中文
			String rex2 = "[\u4e00-\u9fa5]+";
			Matcher m2 = Pattern.compile(rex2, Pattern.DOTALL).matcher(str);
			if (m2.find()) {
				return m2.group();
			}
		}
		return null;
	}

	private static String getIndexContent(String html) throws Exception {
		String rex = "<blockquote>(.*?)</blockquote>";
		Matcher m = Pattern.compile(rex, Pattern.DOTALL).matcher(html);
		if (m.find()) {
			String str = m.group();
			int index = str.indexOf(">");
			str = str.substring(index + 1);
			index = str.indexOf("<");
			str = str.substring(0, index);
			return str.trim();
		}
		return null;
	}

	public static void main(String[] args) throws Exception {
		String cityId = "101010100";
		System.out.println(getWeather(cityId));
	}
}
